<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
use think\facade\Session;
use think\facade\Config;
use think\Db;

class Dan extends Base
{
    public function index(){
        $class_id = request()->param()['class_id'];
        $class_seo=Db::table('lily_classify_seo')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $class_info=Db::table('lily_classify')->where([['status','=',1],['id','=',$class_id]])->find();
        $dan_view=Db::table('lily_classify_dan_view')->field('view_name,content')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $data['classify_content']=Db::table('lily_classify_content')->where('class_id','=',$class_id)->find();/*分类的内容信息*/
        $data['seo_title']=$class_seo['title'];
        $data['seo_keywords']=$class_seo['keywords'];
        $data['seo_desc']=$class_seo['description'];
        $data['dan_view']=$dan_view;
        $data['class_info']=$class_info;
        $data['base_data']=$this->base_data;
        $crumbs=get_crumbs($class_id);/*面包屑*/  
        $crumbs_data=array();
        foreach ($crumbs as $key => $value) {
            if($value['cname']!='Resources'){
               $crumbs_data[]=$value;
            }
        }
        $data['crumbs']=$crumbs_data;
        $this->assign($data);
        if(!empty($dan_view['view_name'])){
           return $this->fetch($dan_view['view_name']);
        }
        if(!empty($dan_view['content'])){
           return $this->fetch('zidingyi');
        }
    }

    /*产品总页面特殊处理*/
    public function products(){
        $class_id = request()->param()['class_id'];
        $product_class_data=[];

        /*产品根路径*/
        $class=Db::table('lily_classify')->where([['status','=',1],['id','=',$class_id]])->find();
        if(!empty($class['route_name'])){
            $class_url=$class['route_name'];
        }else{
            $class_url=change_lower_symbol($class['cname']);
        }
        $array = [
            'Ball Bearings'
            ,'Roller Bearings'
            ,'Cam Followers'
            ,'Plain Bearings'
            ,'Linear Bearings'
            ,'Ceramic Bearings'
            ,'Stainless Steel Bearings'
            ,'Guide Wheel'
            ,'Polyurethane Rollers'
            ,'Accessories'
            ,'Custom Bearings'
            ,'Bearing Applications' 
        ];
        foreach($array as $key=>$cname){
            //  第一级
            $first=Db::table('lily_classify')->where([['status','=',1],['fid','=',$class_id],['is_tag','=',0],['is_show','=',1], ['cname', '=', $cname]])->order('seq','asc')->find();
            $first['url'] = get_class_uplower_url($first['id']);
            $product_class_data[$key]['first']=$first;
            
            // 第二级
            $class_tag=$class_notag=[];
            $second=Db::table('lily_classify')->where([['status','=',1],['fid','=',$first['id']],['is_show','=',1]])->order('seq','asc')->select();
            if(!empty($second)){
                foreach ($second as $k => $v) {
                    if($v['is_tag']){
                        $tag_id = $v['id'];
                        $tag_name = $v['alias_name'];
                        $tag_class = Db::table('lily_classify')->where([['status','=',1],['fid','=',$tag_id],['is_show','=',1]])->order('seq')->select();
                        foreach($tag_class as $vv){
                            $vv['url'] = get_class_uplower_url($vv['id']);
                            $class_tag[$tag_name][] = $vv;
                        }
                    }else{
                        $v['url'] = get_class_uplower_url($v['id']);
                        $class_notag[] = $v;
                    }
                }
                $product_class_data[$key]['second']['class_notag'] = $class_notag;
                $product_class_data[$key]['second']['class_tag']   = $class_tag;
            }

        }

        $class_seo=Db::table('lily_classify_seo')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $dan_view=Db::table('lily_classify_dan_view')->field('view_name')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $data['seo_title']=$class_seo['title'];
        $data['seo_keywords']=$class_seo['keywords'];
        $data['seo_desc']=$class_seo['description'];
        $data['base_data']=$this->base_data;
        $data['product_class_data']=$product_class_data;

        $this->assign($data);
        if(!empty($dan_view)){
            return $this->fetch($dan_view['view_name']);
        }
    }

    /*产品总页面特殊处理*/
    public function products1(){
        $class_id = request()->param()['class_id'];
        $product_class_data=[];

        /*产品根路径*/
        $class=Db::table('lily_classify')->where([['status','=',1],['id','=',$class_id]])->find();
        if(!empty($class['route_name'])){
        	$class_url=$class['route_name'];
        }else{
            $class_url=change_lower_symbol($class['cname']);
        }
        /*获取路径*/
        $first_class=Db::table('lily_classify')->where([['status','=',1],['fid','=',$class_id],['is_tag','=',0],['is_show','=',1]])->order('seq','asc')->select();
        foreach ($first_class as $key => $value) {
        	if(!empty($value['route_name'])){
        		$first_url=$class_url.'/'.$value['route_name'];
        	}else{
                $first_url=$class_url.'/'.change_lower_symbol($value['cname']);
        	}
        	$value['url']=$first_url;
            $product_class_data[$key]['first']=$value;

        	$second=Db::table('lily_classify')->where([['status','=',1],['fid','=',$value['id']],['is_tag','=',0],['is_show','=',1]])->order('seq','asc')->select();
        	if(!empty($second)){
        		foreach ($second as $k => $v) {
        			if(!empty($v['route_name'])){
		        		$second_url=$first_url."/".$v['route_name'];
		        	}else{
		                $second_url=$first_url."/".change_lower_symbol($v['cname']);
		        	}
		        	$second[$k]['url']=$second_url;
        		}
        		$product_class_data[$key]['second']=$second;
        	}
        }
        
        $class_seo=Db::table('lily_classify_seo')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $dan_view=Db::table('lily_classify_dan_view')->field('view_name')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $data['seo_title']=$class_seo['title'];
        $data['seo_keywords']=$class_seo['keywords'];
        $data['seo_desc']=$class_seo['description'];
        $data['base_data']=$this->base_data;
        $data['product_class_data']=$product_class_data;

        $this->assign($data);
        if(!empty($dan_view)){
            return $this->fetch('products1');
        }
    }
    public function test(){
        $series      = request()->param()['series']; //系列：6003/6005/6802/6803/6806/6902/6903/608/688/
        $url_product = request()->param()['part_number'];
        
        $sql = "select class_id, part_number, url_product 
                from lily_products 
                where (part_number like '% $series' OR part_number like '% $series 2RS' OR part_number='{$series}C') 
                    and FIND_IN_SET(class_id, '640,644,648,701')
                union 
       
                select class_id, part_number, url_product
                from lily_products 
                where id in(
                    select MAX(id) from lily_products where part_number LIKE '%$series%2RS%' and class_id=701 and id in(
                    select pid from lily_products_detail where class_id=701 and `key`='Seal Type' and `value`!='Open'
                    )
                )";
        $res = Db::query($sql);

        $data['series']['PN'] = $series;
        foreach($res as $rw){
            $rw['url_product'] = current(explode('_', $rw['url_product']));
            if(strpos($rw['part_number'], 'CEZR')!==false){
                $data['series']['CEZR'][] = $rw;
            }else if(strpos($rw['part_number'], 'CESI')!==false){
                $data['series']['CESI'][] = $rw;
            }else if(strpos($rw['part_number'], 'CESC')!==false){
                $data['series']['CESC'][] = $rw;
            }else{
                $data['series']['Hybrid'][] = $rw;
            }
            if($rw['url_product'] == $url_product){
                $part_number = $rw['part_number'];
            }
        } 
       
        //获取详情页数据
        $data['zhu'] = Db::table('lily_products')->where([['part_number','=',$part_number], ['class_id', 'in', [640,644,648,701]]])->find();
        $pid      = $data['zhu']['id'];
        $class_id = $data['zhu']['class_id'];

        $detail = Db::table('lily_products_detail')->field('`key`, `value`')->where([['pid','=', $pid], ['is_show', '=', 1]])->select();
        foreach($detail as $v){
            $key = $v['key'];
            $val = $v['value'];
            if($key=='System of Measurement'){
                continue;
            }else if($key=='Seal Type' || $key=='Ring Material'){
                if($key=='Seal Type'){
                    $data['zhu'][$key] = ($val == 'Open') ? 'Open':'Sealed';
                }else{
                    $data['zhu'][$key] = $class_id=='701' ? 'Hybrid Ceramic':$val;
                }
            }else{
                $data['detail'][$key]=$val;
            }
        }
        //获取面包屑
        $data['crumbs'] = get_crumbs($class_id);
        
        //获取推荐型号
        $data['recommend'] = Db::table('lily_products')->field('part_number, url_product, img')->where('id','>',$pid)->limit(5)->select();

        
        //seo
        $seo=Db::table('lily_classify_seo')->where([['status','=',1],['class_id','=',$class_id]])->find();
        $data['seo_title']    = $data['zhu']['part_number'] . ' Ceramic Bearing';
        $data['seo_keywords'] = $seo['keywords'];
        $data['seo_desc']     = $seo['description'];

        $data['base_data']=$this->base_data;
        $this->assign($data);
        return $this->fetch('ce6902');
    }
    public function spindle_bearings_search(Request $request){
        $Seal_Types = $request->post()['Seal_Types'] ?? 'Open';
        $Bore_Dia   = $request->post()['Bore_Dia'] ?? '';
        $Outer_Dia  = $request->post()['Outer_Dia'] ?? '';
        $Width      = $request->post()['Width'] ?? '';
        
        $pid_list = [];
        if(!!$Bore_Dia){
            $sql = " select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Bore Dia' and `value`='$Bore_Dia' group by pid ";
            $filter = Db::query($sql);
            $pid_list = array_column($filter, 'pid');
        }
        
        if(!!$Outer_Dia){
            $sql = " select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Outer Dia' and `value`='$Outer_Dia' group by pid ";
            $filter = Db::query($sql);
            $pid_list2 = array_column($filter, 'pid');
            $pid_list = array_intersect($pid_list, $pid_list2);
        }

        if(!!$Width){
            $sql = " select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Width' and `value`='$Width' group by pid ";
            $filter = Db::query($sql);
            $pid_list3 = array_column($filter, 'pid');
            $pid_list = array_intersect($pid_list, $pid_list3);
        }

        $whereStr = '';
        if(!empty($pid_list)){
            $pids = implode(',', $pid_list);
            $whereStr = " and a.pid in($pids)";
        }
        $sql1=" SELECT a.`value`
                from lily_products_detail a
                inner join (
                    select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Seal Type' and `value`='$Seal_Types' group by pid
                ) b on b.pid=a.pid
                where a.class_id in (63,64,65,66)
                    and a.`key`='Bore Dia'
                    $whereStr
                group by a.`value`
                order by a.`value`+0 asc";
        $data['Bore_Dia'] = Db::query($sql1);


        $sql1=" SELECT a.`value`
                from lily_products_detail a
                inner join (
                    select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Seal Type' and `value`='$Seal_Types' group by pid
                ) b on b.pid=a.pid
                where a.class_id in (63,64,65,66)
                    and a.`key`='Outer Dia'
                    $whereStr
                group by a.`value`
                order by a.`value`+0 asc";
        $data['Outer_Dia'] = Db::query($sql1);

        $sql1=" SELECT a.`value`
                from lily_products_detail a
                inner join (
                    select pid from lily_products_detail where class_id in (63,64,65,66) and `key`='Seal Type' and `value`='$Seal_Types' group by pid
                ) b on b.pid=a.pid
                where a.class_id in (63,64,65,66)
                    and a.`key`='Width'
                    $whereStr
                group by a.`value`
                order by a.`value`+0 asc";
        $data['Width'] = Db::query($sql1);

        return json($data);
    }

    

}